<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>编辑会员</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        #thumb-box {
            position: relative;
        }

        #thumb {
            width: 200px;
            height: 290px;
            position: absolute;
            left: 328px;
            top: -252px;
            display: none;
        }
    </style>
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <input type="hidden" id="memberId" name="memberId" value="${editMember.memberId}">
        <input type="hidden" id="memberRoleCode" name="memberRoleCode" value="${editMember.memberRoleCode}">
        <div class="layui-form-item">
            <label for="memberName" class="layui-form-label">
                <span class="x-red">*</span>会员名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="memberName" name="memberName" value="${editMember.memberName}" required=""
                       lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="memberPhone" class="layui-form-label">
                <span class="x-red">*</span>手机号码
            </label>
            <div class="layui-input-inline">
                <input type="number" id="memberPhone" name="memberPhone" value="${editMember.memberPhone}" required=""
                       lay-verify="phone"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>状态
            </label>
            <div class="layui-input-inline">
                <select name="memberStatus">
                    <option value="1" ${editMember.memberStatus == 1 ? 'selected' : ''}>正常</option>
                    <option value="2" ${editMember.memberStatus == 2 ? 'selected' : ''}>挂失</option>
                    <option value="3" ${editMember.memberStatus == 3 ? 'selected' : ''}>冻结</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="oldMemberPassword" class="layui-form-label">
                <span class="x-red">*</span>原密码
            </label>
            <div class="layui-input-inline">
                <input type="password" id="oldMemberPassword" name="oldMemberPassword" required="" lay-verify="password"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <a class="layui-btn layui-btn-normal" id="showBtn" onclick="showPasswordPart()">修改密码</a>
        </div>
        <div id="passwordPart">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button class="layui-btn" lay-filter="edit" lay-submit="">
                确认编辑
            </button>
        </div>
    </form>
    <label class="layui-form-label"></label>
    <button class="layui-btn layui-bg-red layui-btn-primary" id="forgetPasswordBtn" onclick="showForgetPassword()">
        忘记密码
    </button>
    <br/>
    <br/>
    <label class="layui-form-label"></label>
    <button class="layui-btn layui-bg-orange" style="display: none" id="sendCodeBtn" onclick="sendCode()">发送验证码
    </button>
    <div class="layui-form-item">
        <input type="hidden" id="forgetMemberId" name="forgetMemberId" value="${editMember.memberId}">
        <label class="layui-form-label"></label>
        <div id="forgetPassword"></div>
        <label class="layui-form-label"></label>
        <button class="layui-btn" id="submitCode" STYLE="display: none">
            提交&重置
        </button>
    </div>
</div>
<script>
    var IntervalTime;//timer变量，控制时间
    var count = 60;//间隔函数，1秒执行
    var surcount;//当前剩余秒数
    function sendCode() {
        surcount = count;
        $("#sendCodeBtn").addClass("layui-bg-gray layui-btn-disabled").attr("disabled", true);
        $("#sendCodeBtn").text(surcount + "秒后可重新发送");
        IntervalTime = window.setInterval(SetReTimer, 1000)
    }

    function SetReTimer() {
        if (surcount == 0) {
            window.clearInterval(IntervalTime);//停止计时器
            $("#sendCodeBtn").removeClass("layui-bg-gray layui-btn-disabled").attr("disabled", false);
            $("#sendCodeBtn").text("重新发送验证码");
        } else {
            surcount--;
            $("#sendCodeBtn").text(surcount + "秒后可重新发送");
        }
    }

    //知道原密码修改密码
    var isShow = false;

    function showPasswordPart() {
        if (!isShow) {
            $("#passwordPart").append('<div class="layui-form-item" disabled="disabled">\n' +
                '                <label for="memberPassword" class="layui-form-label">\n' +
                '                    新密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="memberPassword" name="memberPassword" value="" lay-verify="password"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="layui-form-item">\n' +
                '                <label for="memberPassword2" class="layui-form-label">\n' +
                '                    确认密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="memberPassword2" name="memberPassword2" value="" lay-verify="password2"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>')
            $("#showBtn").text("隐藏修改密码");
            isShow = true;
        } else {
            $("#passwordPart").empty();
            $("#showBtn").text("修改密码");
            isShow = false;
        }
    };


    //忘记密码
    var isForget = false;

    function showForgetPassword() {
        if (!isForget) {
            $("#forgetPassword").append(
                '<div class="layui-form-item" disabled="disabled">\n' +
                '                <label for="Code" class="layui-form-label">\n' +
                '                    输入验证码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="number" id="Code" name="Code" value="" lay-verify="Code"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>\n' +
                '<div class="layui-form-item" disabled="disabled">\n' +
                '                <label for="resetPassword" class="layui-form-label">\n' +
                '                    重置密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="resetPassword" name="resetPassword" value="" lay-verify="resetpassword"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>\n' +
                '            <div class="layui-form-item">\n' +
                '                <label for="resetPassword2" class="layui-form-label">\n' +
                '                    确认密码\n' +
                '                </label>\n' +
                '                <div class="layui-input-inline">\n' +
                '                    <input type="password" id="resetPassword2" name="resetPassword2" value="" lay-verify="resetpassword2"\n' +
                '                           autocomplete="off" class="layui-input">\n' +
                '                </div>\n' +
                '            </div>')
            $("#forgetPasswordBtn").addClass("layui-bg-blue");
            $("#forgetPasswordBtn").text("想起密码了");
            $("#sendCodeBtn").show();
            $("#submitCode").show();
            isForget = true;

        } else {
            $("#forgetPassword").empty();
            $("#sendCodeBtn").text("发送验证码");
            $("#forgetPasswordBtn").removeClass("layui-bg-blue");
            $("#forgetPasswordBtn").text("忘记密码");
            $("#submitCode").hide();
            $("#sendCodeBtn").hide();
            isForget = false;
        }
    };

    var relcode = ""
    $("#sendCodeBtn").click(function () {
        var phone = $("#memberPhone").val()
        var loading = layer.load(2, {
            shade: [0.8, '#fff'],
            content:'验证码发送中...',
            success: function (layerContentStyle) { // 设置loading样式
                layerContentStyle.find('.layui-layer-content').css({
                    'padding-left': '35px',
                    'color':'#FF3366',
                    'text-align': 'left',
                    'width': '175px',
                    'line-height':'30px'
                });
            }
        });
        $.ajax({
            async: false,
            type: "post",
            url: "${pageContext.request.contextPath}/member?m=sendCodeByMemberPhone",
            data: {
                forgetMemberPhone: phone
            },
            dataType: "json",
            success: function (data) {
                if (data.code === 200) {
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 1});
                    relcode = data.data;
                }else {
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 5})
                }
            }
        });
    });
    $("#submitCode").click(function () {
        if (checkCode()) {
            if (checkResetPassword()){
                //发异步，把数据提交给java
                $.ajax({
                    async: false,
                    type: "post",
                    url: "${pageContext.request.contextPath}/member?m=resetPasswordByForgetWorkerPhone",
                    data: {
                        forgetMemberId: $("#forgetMemberId").val(),
                        resetPassword: $("#resetPassword").val()
                    },
                    success: function (data) {
                        layer.msg(data.msg, function () {
                            if (data.code === 200) {
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        });
                    }
                });
            }
        }
    })

    function checkCode() {
        var res = /^\d{6}$/;
        var code = $("#Code").val();//短信验证码
        if (res.test(code)) {
            if (relcode == code) {
                return true;
            } else {
                layer.msg('验证码错误', {icon: 5});
                return false;
            }
        } else {
            layer.msg('请输入正确的验证码', {icon: 5});
            return false;
        }
    }
    function checkResetPassword() {
        var resetPassword = $("#resetPassword").val();
        var resetPassword2 = $("#resetPassword2").val();
        if (checkPwd(resetPassword)&&checkPwd(resetPassword2)){
            if (resetPassword==resetPassword2){
                return true;
            }else {
                layer.msg('两次密码不一致',{icon:5})
                return false;
            }
        }{
            layer.msg('密码必须6-12位',{icon:5})
            return false;
        }
    }

    function checkPwd(str) {
        var reg = /^(.+){6,12}/
        if (reg.test(str)) {
            return true;
        } else {
            return false;
        }
    }

    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        if ($('#memberPassword').val() != null || $('#memberPassword').val() != "") {
            //自定义验证规则
            form.verify({
                password: [/(.+){6,12}$/, '密码必须6到12位']
                , password2: function () {
                    if ($('#memberPassword').val() != $('#memberPassword2').val()) {
                        return '两次密码不一致';
                    }
                }
            });
        }


        //监听提交
        form.on('submit(edit)', function (data) {
            //发异步，把数据提交给java
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/member?m=edit",
                data: data.field,
                dataType: "json",
                success: function (data) {
                    layer.msg(data.msg, function () {
                        if (data.code === 200) {
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }
                    });

                }
            });
            return false;
        });

    });

</script>

</body>

</html>